'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 副本
 * @author sundbcn@gmail.com (sun debing)
 */

var _ = require('lodash');
var copyConf = require('../config/copyConf');
var buildType = require('../consts/buildType');
var buildTypeUtil = require('../util/buildTypeUtil');
var buildConf = require('../config/buildConf');
var taskService = require('../services/taskService');
var copyConditionCheck = require('../libs/copyConditionCheck');

/**
 * 通关副本
 * @param  {[type]} copy_id [description]
 * @return {[type]}         [description]
 */
exports.passCopy = function (player_manager, copy_id, star) {
    var copy_model = player_manager.copy;
    copy_model.passCopy(copy_id, star);

    // 增加通关的任务统计
    if (star >= 1) {
        taskService.addPassCopy(player_manager, copy_id);
    }

    if (star == 3) {
        taskService.addStar3Copy(player_manager, copy_id);
    }

    //taskService.addPassCopyNum(player_manager);
};

/**
 * 获取所有概率的总和
 * @param  {[type]} copy_id [description]
 * @return {[type]}         [description]
 */
exports.getTotalLostProportion = function (copy_id) {
    var copy_build_conf = copyConf.getCopyBuildConf(copy_id);
    var lost_proportion_total = 0;
    _.each(copy_build_conf, function (build_conf) {
        // 如果是路的话路过
        if (build_conf.building_id == buildType.ROAD) {
            return;
        }

        var build_type = buildTypeUtil.getBuildType(build_conf.building_id);
        var building_type_conf = buildConf.getBuildTypeConf(build_type);
        if (!!building_type_conf) {
            lost_proportion_total += building_type_conf.lost_proportion;
        }
    });
    return lost_proportion_total;
};

/**
 * 获取星级
 * @param  {[type]} copy_id [description]
 * @return {[type]}         [description]
 */
exports.getStarProgress = function (copy_record, damage_build_list, data) {
    var copy_condition_list = copyConf.getCopyCondition(copy_record.copy_id);
    _.each(copy_condition_list, function (copy_condition) {
        var condition_progress = copyConditionCheck.getProgress(copy_condition, copy_record, damage_build_list, data);
        copy_condition.total_progress = condition_progress.total_progress;
        copy_condition.progress = condition_progress.progress;
        copy_condition.is_pass = condition_progress.is_pass;
    });
    return copy_condition_list;
};
